package com.abhishekduhoon.tfis;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.abhishekduhoon.tfis.Methods;

public class ExactMatchScan extends HttpServlet {
    
	private static final long serialVersionUID = 1L;
	
    protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        
    	String userSequence = request.getParameter("userSequence");
    	String transcriptionFactorBindingSequence = request.getParameter("transcriptionFactorBindingSequence");
    	String error = "error.jsp";
    	
    	if(userSequence.isEmpty() || transcriptionFactorBindingSequence.isEmpty()){
    		response.sendRedirect(error);
    	}
    	
    	else{
    
        userSequence = Methods.reformSequence(userSequence);
        String userSequenceCopy = userSequence;
        String colorCodeStart = "<font color='red'>";
        String colorCodeEnd = "</font>";
    	int userSequenceLength = userSequence.length();
    	int transcriptionFactorLength = transcriptionFactorBindingSequence.length();
    	int counter = 0;
    	int totalDetection = 0;
    	int index;
        int indexCopy;

        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<meta charset='UTF-8'>");
            out.println("<title>TFIS v1.0</title>");
            out.println("<meta name='description' content='Transcription Factor Information System'>");
            out.println("<meta name='keywords' content='TFIS, TF, TFBS'>");
            out.println("<link href='css/style.css' rel='stylesheet' type='text/css'>");
            out.println("</head>");
            out.println("<body>");
            out.println("<div id='wrapper'>");
            out.println("<div id='header'>");
            out.println("<div class='top_banner'>");
            out.println("<h1>TFIS v1.0</h1>");
            out.println("<p>Transcription Factor Information System</p>");
            out.println("</div>");
            out.println("</div>");
            out.println("<div id='page_content'>");
            out.println("<div class='navigation'>");
            out.println("<ul>");
            out.println("<li><a href='index.jsp'>Home</a></li>");
            out.println("<li><a href='about.jsp'>About</a></li>");
            out.println("<li><a href='doc.jsp'>Documentation</a></li>");
            out.println("<li><a href='feedback.jsp'>Feedback</a></li>");
            out.println("<li><a href='support.jsp'>Support</a></li>");
            out.println("</ul>");
            out.println("</div>");
            out.println("<div class='main_section'>");
            out.println("<h2>TFIS v1.0 : Result</h2>");
            out.println("<hr>");
            out.println("<div class='common_content'>");
            out.println("<h2>Sequence Length : " + userSequenceLength + "</h2>");
            out.println("<h2>Transcription Factor (TF) Length : " + transcriptionFactorLength + "</h2>");
            out.println("<hr>");
            out.println("<h2>Positions Of Detected Transcription Factor Binding Sites (TFBSs) are : ");
            out.println("<br>");
            
            while(counter <= userSequenceLength - transcriptionFactorLength){
                index = userSequence.indexOf(transcriptionFactorBindingSequence, counter);
                if(index >= 0){
                    indexCopy = index + totalDetection * 25;
                    userSequenceCopy = Methods.insertCode(userSequenceCopy, colorCodeStart, indexCopy);
                    indexCopy = indexCopy + transcriptionFactorLength + 18;
                    userSequenceCopy = Methods.insertCode(userSequenceCopy, colorCodeEnd, indexCopy);
                    index++;
                    totalDetection++;
                    out.println("<h2>" + totalDetection + " -> " + index + "</h2>");
                    counter = index + transcriptionFactorLength - 1;
                }
                else{
                    break;
                }
            }
            
            out.println("<h2>Total Transcription Factor Binding Sites (TFBSs) Detected : " + totalDetection + "</h2>");
            out.println("<hr>");
            out.println("<h2>FASTA Format :</h2>");
            out.println("<span><h3>"+userSequenceCopy+"</h3></span>");
            out.println("</div>");
            out.println("</div>");
            out.println("<div id='footer'>Sengupta <i>et al.</i> | Developed and Maintained by <a href='http://www.abhishekduhoon.com' target='_blank'>Abhishek Kumar</a>.");
            out.println("</div>");
            out.println("</div>");
            out.println("</div>");
            out.println("</body>");
            out.println("</html>");
        }
    	}
    }
	
}